<?php

namespace app\admin\services;

use think\App;
use think\facade\Cache;
use think\facade\Request;
use think\Service;
use think\facade\Session;
use app\admin\model\AdminUserModel;
use app\admin\model\AuthgroupaccessModel;
use app\admin\model\OperationModel;
use think\facade\Config;
class UserService extends Service
{
    public function __construct(Request $request = null, App $app = null)
    {
        parent::__construct($request, $app);
           $this->AdminUserModel = new AdminUserModel();
           $this->AuthgroupaccessModel = new AuthgroupaccessModel();
           $this->OperationModel = new OperationModel();
    }


    /**
     * @param 用户列表
     */
    public function userlist(){
        $post = Request::param();
        $page = isset($post['pageIndex'])?$post['pageIndex']:1;
        $limit = isset($post['pageSize'])?$post['pageSize']:10;
        if(!empty($post['username'])){
            $where = [['username','like','%'.$post['username'].'%'],['isdelete','=',1],['username','neq','admin']];
        }else{
            $where=[['isdelete','=',1],['username','neq','admin']];
        }
        $list = $this->AdminUserModel->userlist($where,$page,$limit);
        $counts = $this->AdminUserModel->county($where);
        $array = [];
        foreach ($list as $value){
            $key = 'login_id'.$value['id'];
            $uesdata = Cache::get($key);
            $value['stype'] = empty($uesdata)?"离线":"在线";
            $value['username'] = Session::get('uid','admin')==1?$value['username']."(".$value['name'].")":$value['username'];
            $value['loginTime'] = empty($value['loginTime'])?"":$value['loginTime'];
            $value['loginIp'] = empty($value['loginIp'])?"":$value['loginIp'];
            $value['phone'] = empty($value['phone'])?"":$value['phone'];
            $value['stuts'] = disa($value['disable']);
            $value['ztet'] =  opstion($value['position']);
            $array[] = $value;
        }
        if(empty($list)){
            $data['rel'] = false;
            $data['msg'] = "暂无数据";
        }else{
            $data['rel'] = true;
            $data['msg'] = "获取成功";
        }
        $data['list'] = $array;
        $data['count'] = $counts;
       return $data;
    }


    /**
     * @param 添加用户
     */
    public function adduser(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        $confighd = Config::get('ADMIN_HEADER');
        $configft = Config::get('ADMIN_FOOTER');
        $username = $post['username'];
        $authgroup = $post['sole'];
        $position = $post['position'];
        $phone = $post['phone'];
        $name = empty($post['names'])?"":$post['names'];
        if(!preg_match("/^0?(13|14|15|17|18)[0-9]{9}$/",$phone)){
            echo "请输入有效的手机号码!";
        }
        $password = md5($confighd.$post['password'].$configft);
        $where = [
            ['username', '=', $username]
        ];
        $user = $this->AdminUserModel->findPw($where);
        if($user){
            return $this->_getResponse("USER_ERROR");
        }
        if($position==3){
            $data['special'] = 2;
        }
        $data['username'] = $username;
        $data['password'] = $password;
        $data['authgroup'] = $authgroup;
        $data['position'] = $position;
        $data['phone'] = $phone;
        $data['name'] = $name;
        $useradd = $this->AdminUserModel->adduser($data);
        if(!$useradd){
            return $this->_getResponse("NUKNOW_EORRE");
        }

        $guopr = [
            'uid' => $useradd,
            'group_id' => $authgroup
        ];

        $authoup = $this->AuthgroupaccessModel->useradd($guopr);
        if(!$authoup){
            return $this->_getResponse("NUKNOW_EORRE");
        }
        return $this->_getResponse("USER_ADD");
    }


    /**
     * @param 禁用账户/启用账户
     */
    public function examine(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        $where = [['id','eq',$post['id']]];
        $table = $this->AdminUserModel->findPw($where);
        $disable = $table['disable'];
        if($disable==1){
           $data = [
               'disable'=> 2
           ];
            $oper['actionInfo']= "禁用用户";
        }else{
            $data = [
                'disable'=> 1
            ];
            $oper['actionInfo']= "启用用户";
        }
        $rustl = $this->AdminUserModel->examineTs($post['id'],$data);
        if(!$rustl){
            return $this->_getResponse("PARAM_EORRE");
        }
        $uid = Session::get('uid','admin');
        $oper['actionTime'] = time();
        $oper['actionIp'] = getIp();
        $oper['uid'] = $uid;
        $oper['username'] = Session::get('username','admin');
        $alllog = $this->OperationModel->addlog($oper);
        if(!$alllog){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("ENBES_DATE");
    }


    /**
     * @param 修改用户
     */
    public function edit(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        if(empty($post['password'])){
            return ["status"=> 1,"msg"=>"密码不能为空"];
        }
        if(empty($post['newpassword'])){
            return ["status"=> 1,"msg"=>"请确认密码"];
        }
        if(empty($post['uid'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        if($post['password']!=$post['newpassword']){
            return ["status"=> 1,"msg"=>"密码不一致"];
        }
        $confighd = Config::get('ADMIN_HEADER');
        $configft = Config::get('ADMIN_FOOTER');
        $password = md5($confighd.$post['password'].$configft);
        $uid = $post['uid'];
        $data = [
            'password' => $password
        ];
        $this->AdminUserModel->edit($uid,$data);
        return $this->_getResponse("ENBES_DATE");
    }


    /**
     * @param 删除用户
     */
    public function delelt(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        if(empty($post['uid'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        $id = $post['uid'];
        $where = [['id','eq',$id]];
        $data = [
            'isdelete'=> 2
        ];
        $delete = $this->AdminUserModel->dele($where,$data);
        if(!$delete){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("DELETE_DATE");
    }


    /**
     * @param 分配类型
     */
    public function versio(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        $id = Session::get('uid','admin');
        if(empty($post['uid'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        $list = empty($post['data'])?"":$post['data'];
        $list = explode('&',$list);
        $array = [];
        foreach ($list as $value){
            $array[] = substr($value,6);
        }
        $verty = implode(',',$array);
        $data = ['Versiontype'=>empty($post['data'])?"":$verty];
        $where = [['id','eq',$post['uid']]];
        $rulset = $this->AdminUserModel->versio($where,$data);
        if(empty($rulset)){
            return $this->_getResponse("UPDATES_EORRE");
        }
        if($post['uid']==$id){
            Session::set('Versiontype',$verty, 'admin');
        }
        return $this->_getResponse("ENBES_DATE");
    }


    /**
     * @param 手册分类
     */
    public function intery(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        $id = Session::get('uid','admin');
        if(empty($post['data']) || empty($post['uid'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        $list = $post['data'];
        $list = explode('&',$list);
        $array = [];
        foreach ($list as $value){
            $array[] = substr($value,6);
        }
        $intery = implode(',',$array);
        $data = ['manual'=>$intery];
        $where = [['id','eq',$post['uid']]];
        $rulset = $this->AdminUserModel->versio($where,$data);
        if(!$rulset){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        if($post['uid']==$id){
            Session::set('manual',$intery, 'admin');
        }
        return $this->_getResponse("ENBES_DATE");
    }


    /**
     * @param 获取当前分类
     */
    public function verss(){
        $uid = Request::param('id');
        $where = [['id','eq',$uid]];
        $ziduan = 'Versiontype';
        $rulst = $this->AdminUserModel->Verasc($where,$ziduan);
        $ver = explode(',',$rulst['Versiontype']);
       return $ver;
    }


    /**
     * @param 获取当手册分类
     */
    public function intert(){
        $uid = Request::param('ids');
        $where = [['id','eq',$uid]];
        $ziduan = 'manual';
        $rulst = $this->AdminUserModel->Verasc($where,$ziduan);
        $ver = explode(',',$rulst['manual']);
        return $ver;
    }


    /**
     * @param $key
     * @param array $data
     * @return mixed
     */
    protected function _getResponse($key, $data = [])
    {
        $maps = [
            "SUCCESS" => ["status" => 0, "msg" => "sccuess", "data" => $data],
            "USER_ADD" => ["status" => 0, "msg" => "添加成功", "data" => $data],
            "ENBES_DATE" => ["status" => 0, "msg" => "操作成功", "data" => $data],
            "DELETE_DATE" => ["status" => 0, "msg" => "删除成功", "data" => $data],
            "PARAM_EORRE" => ["status" => 1, "msg" => "参数错误", "data" => $data],
            "UPDATES_EORRE" => ["status" => 1, "msg" => "没有更新数据", "data" => $data],
            "NUKNOW_EORRE" => ["status" => 2, "msg" => "未知错误", "data" => $data],
            "AUTH_EORRE" => ["status" => 3, "msg" => "权限不足", "data" => $data],
            "NO_POST" => ["status" => 4, "msg" => "非法请求", "data" => $data],
            "USER_ERROR" => ["status" => 5, "msg" => "用户名已存在", "data" => $data],
            "CAOZUO_ERROR" => ["status" => 6, "msg" => "操作失败", "data" => $data]

        ];
        return $maps[$key];
    }
}